//
//  ZDAddressListController.m
//  ZDPension
//
//  Created by 刘甲奇 on 2017/6/19.
//  Copyright © 2017年 maker. All rights reserved.
//

#import "ZDAddressListController.h"
#import "ZDAddressListCell.h"

#import "ZDNewAddressController.h"

static NSString *const ZDAddressListCellId = @"ZDAddressListCell";

@interface ZDAddressListController ()

@end

@implementation ZDAddressListController

- (void)setupUI
{
    [super setupUI];
    
    if (self.mode == ManageAddress) {
        self.title = @"收货地址管理";
    }else{
        self.title = @"选择收货地址";
    }
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"新建" style:UIBarButtonItemStylePlain target:self action:@selector(rightClick)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    if (self.mode == ManageAddress)
    {
        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
        self.navigationItem.leftBarButtonItem = leftItem;
    }
    [self.tableView registerNib:[UINib nibWithNibName:@"ZDAddressListCell" bundle:nil] forCellReuseIdentifier:ZDAddressListCellId];
}
- (void)viewWillAppear:(BOOL)animated
{
    [self requestRefreshData];
}

#pragma mark- 网络请求
- (void)requestRefreshData
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    [SVProgressHUD showWithStatus:@"正在加载中"];
    __weak typeof (self) weakSelf = self;
    [API requestPostAppendKey:AddressListUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        ZDLog(@"%s---%d---%@",__func__,__LINE__,successCode);
        NSDictionary *datas = successCode[@"datas"];
        NSArray *address_list = datas[@"address_list"];
        if (address_list.count==0)
        {
            [weakSelf addNoDataViewWithTipText:@"暂时没有地址"];
            [weakSelf.dataSource removeAllObjects];
        }else
        {
            [weakSelf hiddenNoDataView];
            NSArray *tempModel = [ZDAddressModel mj_objectArrayWithKeyValuesArray:address_list];
            [weakSelf.dataSource removeAllObjects];
            [weakSelf.dataSource addObjectsFromArray:tempModel];
            
        }
        [weakSelf.tableView reloadData];
    } falure:^(NSError *er) {
        ZDLog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];

}
- (void)requestDelete:(ZDAddressModel *)model
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:model.address_id forKey:@"address_id"];
    
    [SVProgressHUD showWithStatus:@"正在删除"];
    __weak typeof (self) weakSelf = self;
    [API requestPostAppendKey:DeleteAddressUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        ZDLog(@"%s---%d---%@",__func__,__LINE__,successCode);
        id datas = successCode[@"datas"];
        if ([datas isKindOfClass:[NSDictionary class]])
        {
            NSString *error = datas[@"error"];
            if (error)
            {
                [SVProgressHUD showErrorWithStatus:error];
            }
        }else if ([datas isKindOfClass:[NSString class]])
        {
            [SVProgressHUD showSuccessWithStatus:datas];
            [weakSelf requestRefreshData];
        }

    } falure:^(NSError *er) {
        ZDLog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];

}

#pragma mark- tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZDAddressListCell *cell = [tableView dequeueReusableCellWithIdentifier:ZDAddressListCellId forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
     __weak typeof (self) weakSelf = self;
    [cell addressDeleteClick:^(ZDAddressModel *model) {
        [weakSelf handleDelete:model];
    }];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_mode == SelectAddress)
    {
        ZDAddressModel *model = self.dataSource[indexPath.row];
        [[NSNotificationCenter defaultCenter] postNotificationName:SELECT_ADDRESS_NOTIFICATION object:model];
        [self.navigationController popViewControllerAnimated:YES];
    }
}
#pragma mark- 点击事件
- (void)rightClick
{
     ZDLog(@"%s---%d---点击了新建",__func__,__LINE__);
    ZDNewAddressController *newAddress = [[ZDNewAddressController alloc] init];
    [self.navigationController pushViewController:newAddress animated:YES];
}
- (void)back
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)handleDelete:(ZDAddressModel *)model
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"要删除该地址吗?" preferredStyle:UIAlertControllerStyleAlert];
    __weak typeof (self) weakSelf = self;
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //发送删除请求
        ZDLog(@"%s---%d---删除操作哈",__func__,__LINE__);
        [weakSelf requestDelete:model];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"算了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alertController addAction:sureAction];
    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];

}
@end






